<template>
	<view class="my-settle-container">
		<!-- 全选 -->
		<label class="radio" @click="changeAllState">
			<radio color="#c00000" :checked="isFullCheck" >
				<text>全选</text>
			</radio>
		</label>
		
		<!-- 合计 -->
		<view class="amount-box">
			合计：<text class="amount">￥{{checkedGoodsAmount}}</text>
		</view>
		
		<!-- 结算 -->
		<view class="btn-settle" @click="settlement">
			结算({{checkedCount}})
		</view>
	</view>
</template>

<script>
	import { mapGetters, mapMutations, mapState } from 'vuex'
	
	export default {
		name:"my-settle",
		data() {
			return {
				// 倒计时的秒数
				seconds: 3,
				// 定时器的id
				timer: null,
			};
		},
		computed: {
			// 映射购物车vuex中getters数据里checkedCount()方法到当前页面
			...mapGetters('m_cart', ['checkedCount', 'total', 'checkedGoodsAmount']),
			// 映射用户信息vuex中getters数据里addstr()到当前页面
			...mapGetters('m_user', ['addstr']),
			// 映射用户信息vuex中State里token值到当前页面
			...mapState('m_user', ['token']),
			...mapState('m_cart', ['cart']),
			// 从映射过来的数据中判断购物车商品是否全选
			isFullCheck() {
				// 对比购物车商品数量和结算数量
				return this.total === this.checkedCount
			}, 
		},
		methods: {
			// 将购物车vuex中updateAllGoodsState()方法映射到当前组件
			...mapMutations('m_cart', ['updateAllGoodsState']),
			...mapMutations('m_user', ['updateRedirectInfo']),
			// 通过监听isFullCheck()获取状态信息 触发vuex中updateAllGoodsState()事件改变状态
			changeAllState() {
				this.updateAllGoodsState(!this.isFullCheck)
			}, 
			// 点击结算事件处理
			settlement() {
				// 先对登录账号,结算商品,收货地址进行预验证
				if(!this.checkedCount) return uni.$showMsg('请选择需要结算的商品 ！')
				if(!this.addstr) return uni.$showMsg('请选择收货地址 ！')
				// if(!this.token) return uni.$showMsg('请先登录 ！')
				// 调用this.delayNavigate()方法进行倒计时导航
				if(!this.token) return this.delayNavigate()
				
				// 预验证之后进行订单支付
				this.payOrder()
			},
			// 延迟导航到my页面
			delayNavigate() {
				this.seconds = 3
				this.showTips(this.seconds)
				// 创建定时器
				this.timer = setInterval(() => {
					this.seconds--
					// 判断定时器是否为0
					if (this.seconds <= 0) {
						clearInterval(this.timer)
						// 页面进行跳转
						uni.switchTab({
							url: '/pages/my/my',
							success: () => {
								// 调用用户信息vuex中的updateRedirectInfo方法,将跳转信息存储到Store中
								this.updateRedirectInfo({
									openType: 'switchTab',
									from: '/pages/cart/cart',
								})
							}
						})
						// 当秒数为0时不再显示提示信息
						return
					}
					this.showTips(this.seconds)
				},1000)
			},
			// 展示跳转到登录界面的信息提示
			showTips(n) {
				// 调用uni.showToast()方法展示消息提示
				uni.showToast({
					icon: 'none',
					title: '请登录再结算！' + n + '秒后跳转至登录界面',
					mask: true,
					duration: 1500
				})
			},
			// 创建订单
			async payOrder() {
				// 组织订单的信息对象
				const orderInfo = {
					order_price: 0.01,
					consigneen_addr: this.addstr,
					goods: this.cart.filter(x => x.goods_state).map(x => ({
						goods_id: x.goods_id, 
						goods_number: x.goods_count, 
						goods_price: x.goods_price ,
						}))
				}
				// 发起请求创建订单编号
				const {data: res} = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
				if (res.meta.status !== 200) return uni.$showMsg(res.meta.msg)
				const orderNumber = res.message.order_number
				console.log(orderNumber)
				
				// 发起请求获取订单的支付信息
				const {data: res2} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {order_number: orderNumber})
				if (res2.meta.status !== 200) return uni.$showMsg(res2.meta.msg)
				const payInfo = res2.message.pay()
				
				// 发起微信支付---调用uni.requestPayment()
				const [err, succ] = await uni.requestPaym(payInfo)
				if(err) return uni.$showMsg('订单未支付')
				const {data: res3} = await uni.$http.post('/api/public/v1/my/orders/chkOrder', {order_number: orderNumber}) 
				if (res3.meta.status !== 200) return uni.$showMsg('订单未支付')
				uni.showToast({
					title: '支付完成',
					icon: 'success'
				})
 			}
		}
	}
</script>

<style lang="scss">
.my-settle-container {
	width: 100%;
	height: 50px;
	background-color: white;
	position: fixed;
	bottom: 0;
	left: 0;
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 14px;
	padding-left: 5px;
	.radio {
		display: flex;
		align-items: center;
	}
	.amount-box {
		.amount {
			color: #c00000;
			font-weight: bold;
		}
	}
	.btn-settle {
		background-color: #c00000;
		height: 50px;
		color: white;
		line-height: 50px;
		padding: 0 10px;
		min-width: 100px;
		text-align: center;
	}
}
</style>
